﻿using System.Globalization;
using System.Xml.Linq;
using IcicleFramework.Components;
using IcicleFramework.Entities;

namespace ExampleGame.Components.Score
{
    public abstract class PointsComponent : BaseComponent, IPointsComponent
    {
        public event AddPointsHandler AddPoints;

        public float Points { get; set; }

        protected PointsComponent()
        {
            this.Points = 0.0f;
        }

        protected PointsComponent(PointsComponent old)
        {
            this.Points = old.Points;
        }

        protected virtual void OnAddPoints(IGameObject destination)
        {
            if (AddPoints != null)
                AddPoints(this, destination, this.Points);
        }

        public override void Deserialize(XElement element)
        {
            float points = 0.0f;
            if (element.Element("points") != null)
            {
                float.TryParse(element.Element("points").Value, NumberStyles.Float, CultureInfo.InvariantCulture,
                               out points);
            }
            this.Points = points;
        }

        public override void Dispose()
        {
            AddPoints = null;

            base.Dispose();
        }
    }
}
